<template>
  <div class="monitor_page">

    <!-- table -->
    <el-card class="monitor_page-left" :class="{toggle: isToggleStatus}" shadow="hover">
      <div class="filter_header">
        <el-form ref="form" label-width="100px">
          <div class="monitor_page-filter" v-show="!isToggleStatus">
            <el-form-item label="起止时间：">
              <el-date-picker
                v-show="!isToggleStatus"
                style="width:100%;min-width:240px"
                size="small"
                value-format="timestamp"
                v-model="timerArray"
                type="datetimerange"
                :picker-options="pickerOptions"
                :clearable="false"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </div>
        </el-form>
        <div class="monitor_page-filter flex-between" :class="{setMarginTop: isToggleStatus}">
          <div class="monitor_page-filter-left">
            <button class="toggle_wrap" @click="toggleStatus">
              <i class="el-icon-s-fold" :class="{rotate: isToggleStatus}"></i>
            </button>
            <div v-show="!isToggleStatus">
              <el-button
                size="small"
                @click="confirmDelete"
                icon="el-icon-delete-solid"
                v-if="hasPermission('monitor:record:del')"
                type="danger">删除
              </el-button>
              <el-button
                size="small"
                plain
                v-if="hasPermission('monitor:record:clean')"
                icon="el-icon-s-tools"
                type="primary"
                @click="confirmCleanBaseRecord">设备重置
              </el-button>
              <el-button
                size="small"
                plain
                v-if="hasPermission('monitor:record:mark')"
                type="primary"
                @click="showMonitorRemarkDialog">说明
              </el-button>
              <el-button
                size="small"
                style="margin-top: 10px"
                plain
                v-if="jiuGanKeJi"
                type="primary"
                @click="lookEquipRecord(timerArray,equipId)">运行日志
              </el-button>
              <el-button
                size="small"
                plain
                v-if="hasPermission('monitor:record:export')"
                @click="$emit('export')"
                icon="el-icon-download"
                type="primary">数据导出
              </el-button>
            </div>
          </div>
          <el-button
            v-show="!isToggleStatus"
            size="small"
            type="primary"
            class="common_btn-defalut"
            icon="el-icon-search"
            @click="form.page=1;$emit('update')">搜索
          </el-button>
        </div>
      </div>
      <!-- table -->
      <el-tabs type="border-card" v-model="activeName1" @tab-click="handleClickLeft">
        <el-tab-pane :name="item" :label="item" v-for="(item) in tabsName" :key="item">
          <el-table
            v-show="!isToggleStatus"
            :data="tabLists"
            border
            @selection-change="handleSelectionChange"
            ref="multipleTable"
            tooltip-effect="dark">
            <el-table-column type="selection" width="28">
            </el-table-column>
            <el-table-column prop="createTime" label="数据采集时间" min-width="135">
              <template slot-scope="scope">
                {{scope.row.createTime | formatTimer }}
              </template>
            </el-table-column>
            <el-table-column prop="horizontal" label="水平位移（mm)" min-width="135">
            </el-table-column>
            <el-table-column prop="vertical" label="垂直位移（mm)" min-width="135">
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>

      <!-- 分页器 -->
      <el-pagination
        v-show="!isToggleStatus"
        :disabled="isToggleStatus"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-size="form.pageSize"
        :page-sizes="[10, 20, 30, 40,50,60,70,80,90,100]"
        layout="sizes, prev, pager, next,jumper"
        :total="form.total">
      </el-pagination>
    </el-card>

    <!-- 绘图部分 -->
    <div class="monitor_page-right">
      <el-tabs type="border-card" v-model="activeName2" @tab-click="handleClick">
        <el-tab-pane label="总绘图" name="总绘图">
          <el-card class="card_echarts" shadow="hover">
            <echarts-seven-compose
              ref="testAllVertical" type="testAllVertical"
            ></echarts-seven-compose>

          </el-card>
          <el-card class="card_echarts" shadow="hover">
            <echarts-seven-compose
              ref="testAllHorizontal" type="testAllHorizontal"
            ></echarts-seven-compose>
          </el-card>
        </el-tab-pane>
        <el-tab-pane :name="item" :label="item" v-for="(item) in tabsName" :key="item">
          <el-card class="card_echarts" shadow="hover">
            <echarts-seven-compose2
              ref="tagItem" :type="item" :key="item"
            ></echarts-seven-compose2>
          </el-card>
        </el-tab-pane>
        <el-tab-pane label="图像" name="图像">
          <el-card class="card_echarts" shadow="hover" style="overflow:auto;height: 100%">
            <el-row>
              <el-col :span="8"  v-for="(item,index) in  this.collectionList" :key="index">
          <div v-if="item.imgUrl" style="margin-bottom: 1vw;text-align: center">
            <el-image
              style="width: 90%; cursor: pointer"
              :src="constUrl + item.imgUrl" @click="previewImg(constUrl + item.imgUrl)"
              fit="fill"></el-image>
            <p style="font-weight: bold;font-size: 16px;margin-top: 5px">
              {{item.createTime | formatTimer}}
            </p>
          </div>
              </el-col>
            </el-row>

          </el-card>
        </el-tab-pane>
      </el-tabs>
      <el-card class="card_echarts" shadow="hover">

      </el-card>
    </div>

    <!-- 新增编辑弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="isShowDialog"
      :before-close="handleClose"
      :close-on-click-modal="false">
      <echarts-one-dialog ref="echartsDialog"></echarts-one-dialog>
    </el-dialog>
    <monitor-remark ref="monitorRemark" @update="$emit('update')"></monitor-remark>
    <picture-viewer ref="imageViewer"></picture-viewer>
  </div>
</template>

<script>
  import EchartsSevenCompose2 from "../monitor-echarts/base/echarts-16-compose2";
  import EchartsSevenCompose from "../monitor-echarts/base/echarts-16-compose";
  import ItemMixins from "./item.mixins";
  import Format from '@/utils/format'
  import constUrl from '@/services/env'
  import PictureViewer from '@/components/picture-viewer'

  export default {
    mixins: [ItemMixins],
    components: {EchartsSevenCompose, EchartsSevenCompose2, PictureViewer},
    props: ["typeOneUmumList", "calcType", "calcVal"],
    data() {
      return {
        constUrl,
        tabLists: [],
        tabPanes: [],
        isTestAll: ' ',
        isTest1: true,
        isTest2: true,
        tabsName: [],
        isEcharts: 0,
        activeName1: "P1",
        activeName2: "总绘图",
        activeName: "testAll",
        monitoringsTime: [],
        // 当前的图表钩子
        chartsName: ['testAll',],
        // 分页参数
        form: {
          page: 1,
          pageSize: 10,
          total: 0,
        },
        // 弹窗title
        dialogTitle: "",
        // 是否显示弹窗
        isShowDialog: false,
      };
    },
    filters: {
      // 时间格式化
      formatTimer(timer) {
        return Format(new Date(timer))
      },
    },
    computed: {
      // 筛选类型改变
      detailList() {
        return (
          this.typeOneUmumList.find((x) => x.code === this.calcTypeModel)
            ?.relationTypeList || []
        );
      },
      calcTypeModel: {
        get() {
          return this.calcType;
        },
        set(value) {
          this.$emit("update:calcType", value);
        },
      },
      calcValModel: {
        get() {
          return this.calcVal;
        },
        set(value) {
          this.$emit("update:calcVal", value);
        },
      },
    },
    methods: {
      // 查看大图
      previewImg(url) {
        let list = [url]
        this.$refs.imageViewer.showViewer(list, 0)
      },
      setWarningLine() {
      },
      //
      handleClickLeft(tab) {
        let tabLists = []
        this.collectionList.map(x => {
          x.dataList.map(y => {
            if (y.sensorName == this.activeName1) {
              y.id = x.id
              y.createTime = x.createTime
              tabLists.push(y)
            }
          })
        })
        this.tabLists = tabLists
      },
      //tab 切换进行echart随着窗口改变方法，让数据重新渲染，
      handleClick(tab, event) {
        this.isTestAll = tab.index
        for (let i = 0; i < this.tabsName.length; i++) {
          let a = this.tabsName[i]

          this.$nextTick(() => {
            this.$refs.tagItem[i].echartResize()
          })

        }

        this.$nextTick(() => {
          this.$refs.testAllVertical.echartResize()
          this.$refs.testAllHorizontal.echartResize()
        });
      },
      // 筛选类型改变
      typeChange(value) {
        this.$nextTick(() => {
          this.calcValModel = this.detailList[0]?.code;
        });
      },
      // 数据刷新
      update({data, monitorName, monitorInfo}) {
        this.equipId = monitorInfo.bindEquipId

        this.collectionList = data.recordList || []
        this.form.page = 1;
        this.form.total = this.collectionList.length;
        this.selectionList = JSON.parse(JSON.stringify(this.tableList));
        this.tabsName = []
        this.monitoringsTime = []
        if (data.paintList.length !== 0) {
          let sersorNames = this.selectionList[0].dataList
          sersorNames.forEach((item) => {
            this.tabsName.push(item.sensorName)
          })
          this.activeName1 = this.tabsName[0]
          this.handleClickLeft()
          //处理综合绘图数据
          let verticalList = []
          let horizontalList = []
          data.paintList.map(x => {
            verticalList.push({
              sensorName: x.sensorName, vertical: x.vertical
            })
            horizontalList.push({
              sensorName: x.sensorName, horizontal: x.horizontal
            })
          })
          this.$nextTick(() => {
            this.$refs.testAllVertical.updateEchart(verticalList, '垂直')
            this.$refs.testAllHorizontal.updateEchart(horizontalList, '水平')
          })
          //其他单独绘图的数据
          for (let i = 0; i < this.tabsName.length; i++) {
            let echartsData = {}
            data.paintList.map(x => {
              if (x.sensorName == this.tabsName[i]) {
                echartsData = x
              }
            })
            this.$nextTick(() => {
              this.$refs.tagItem[i].updateEchart(echartsData)
            })

          }
          // this.$refs.laser.updateEchart(echartsData)
        } else if (data.paintList.length === 0) {
          this.tabsName = []
          this.tabPanes = []
          this.$refs.laser.echartClear()
          this.$refs.testAll.echartClear()
        }
      },
      // 当前页码改变
      // handleCurrentChange(val) {
      //   this.form.page = val;
      //   this.$nextTick(() => {
      //     this.$refs.multipleTable.toggleAllSelection();
      //   });
      // },
      // 勾选改变
      handleSelectionChange(val) {
        this.selectionList = val;
        this.partUpdateChart();
      },
      partUpdateChart() {

      },
      handleClose() {
        this.isShowDialog = false;
      },
    },
    mounted() {

    }
  };
</script>

<style scoped="scoped" lang="less">
  @import url("./commom.less");

  // 还需自定义的css
  .card_echarts {
    height: 680px;
  }

  .monitor_page-left {
    // 15px未margin
    min-height: 1530px;
    /*height: 750px;*/
  }

  .filter_select {
    .el-select {
      width: 40%;
    }
  }


  .tabTitle {
    background: red;
  }
</style>
